<template>
  <div class="padding-lr">
    <div class="text-right">
      <el-button type="success" size="mini" @click="save"> 保存 </el-button>
    </div>
    <el-row v-if="isClaimCustomers == true && isOne == false">
      <el-col :span="10">
        <el-form-item label="有效期至" prop="validDate">
          <el-date-picker
            v-model="validDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="选择日期"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="5" class="margin-top" v-if="isOne == true">
<!--      <el-col :span="2" style="margin-top: 4px; margin-left: 10px">-->
<!--        有效期至：</el-col-->
<!--      >-->
      <el-col :span="8">
        <el-form-item label="有效期至" prop="validDate">
          <el-date-picker
            v-model="oneValidDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '100%' }"
            placeholder="选择日期"
            clearable
          ></el-date-picker>
        </el-form-item>
<!--        <el-input-->
<!--          v-model="oneValidDate"-->
<!--          :rows="5"-->
<!--          size="mini"-->
<!--          clearable-->
<!--          :readonly="true"-->
<!--        />-->
      </el-col>
    </el-row>
    <el-row :gutter="5" class="margin-top">
      <el-col :span="2" style="margin-top: 4px; margin-left: 10px">
        备注：</el-col
      >
      <el-col :span="8">
        <el-input v-model="reason" :rows="5" size="mini" clearable />
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { ajax } from "@/store/yg/common/utils";
import Row from "../../../../yg/layout/main/page/wrapper/form/Row";
import { closeDialog } from "@/utils";

export default {
  components: { Row },
  data() {
    return {
      reason: "",
      validDate: "",
      oneValidDate: "",
      isClaimCustomers: false,
      isOne: false,
    };
  },
  mounted() {
    console.log(this.$store.state.edit);
    if (this.$store.state.edit.id !== "") {
      console.log("this.$store.state.edit");
      this.isOne = true;
      if(this.$store.state.edit.row.validDate!=null && this.$store.state.edit.row.validDate!=""){
        console.log(".....")
        this.oneValidDate = this.$store.state.edit.row.validDate;
      }else{
        this.oneValidDate = this.getTime(30);
      }
    }
    var ids = this.$store.state.edit.ids;
    if (ids == null) {
      ids = [];
      ids.push(this.$store.state.edit.id);
    }
    ajax(
      "findByIds",
      {
        ids: ids,
      },
      {
        class: "com.tofba.app.client.customer.CustomerOperateAuditClient",
      }
    ).then(
      (res) => {
        for (let i of res) {
          if (i.type == 0) {
            this.isClaimCustomers = true;
          }
        }
      },
      (error) => {
        console.log(error);
      }
    );

    // let n = -31;
    // let d = (d = new Date());
    // let year = d.getFullYear();
    // let mon = d.getMonth() + 1;
    // let day = d.getDate();
    // if (day <= n) {
    //   if (mon > 1) {
    //     mon = mon - 1;
    //   } else {
    //     year = year - 1;
    //     mon = 12;
    //   }
    // }
    // d.setDate(d.getDate() - n);
    // year = d.getFullYear();
    // mon = d.getMonth() + 1;
    // day = d.getDate();
    // this.validDate =
    //   year +
    //   "-" +
    //   (mon < 10 ? "0" + mon : mon) +
    //   "-" +
    //   (day < 10 ? "0" + day : day);
    //console.log(this.$store.state.edit);
  },
  methods: {
    async save() {
      var ids = this.$store.state.edit.ids;
      if (ids == null) {
        ids = [];
        ids.push(this.$store.state.edit.id);
      }
      if( this.$store.state.edit.row.id!=null && this.oneValidDate==null){
        this.$message.error("有效期不能为空");
      }
      if(this.validDate==null || this.validDate==""){
        this.validDate=this.oneValidDate;
      }
      ajax(
        "batchOperateAudit",
        {
          ids: ids,
          validDate: this.validDate,
          note: this.reason,
        },
        {
          class: "com.tofba.app.client.customer.CustomerOperateAuditClient",
        }
      ).then(
        (res) => {
          if (res.code == 200) {
            this.$message.success("保存成功");
            closeDialog();
            location.reload();
          } else {
            this.$message.error("保存失败:" + res.message);
          }
        },
        (error) => {
          console.log(error);
        }
      );
    },
    getTime(val){
      // 获取前val天的时间
      var dt = new Date;
      val === 0 ? '' : dt.setTime(dt.getTime() + 24 * 60 * 60 * 1000 * val)
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDate() + "").padStart(2, "0");
      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    }
  },
};
</script>

<style scoped>
/deep/ .el-select {
  width: 100%;
}
.btn-group {
  float: right;
  /* margin-left:10px ; */
}
</style>
